package com.carltechnology.mahjonggamecaculation.mode.impl;

import android.content.ContentValues;
import android.database.Cursor;
import android.os.Parcel;
import android.os.Parcelable;

import com.carltechnology.mahjonggamecaculation.mode.IGameRuntimeData;

import java.util.Arrays;

/**
 * Created by WZTCM on 2016/6/28.
 */
public class GameRuntimeDataMode implements IGameRuntimeData,Cloneable {
    private enum DATA_COLUMN_NAME{
        COLUMN_ID("id"),
        COLUMN_COUNT1("count1"),
        COLUMN_COUNT2("count2"),
        COLUMN_COUNT3("count3"),
        COLUMN_COUNT4("count4"),
        COLUMN_COST("cost"),
        COLUMN_MODIFYCOUNT("mcount");
        private String name;
        DATA_COLUMN_NAME(String name){
            this.name = name;
        }

        @Override
        public String toString() {
            return name;
        }
    }
    public static final int MODIFY_MAX = 7;

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        StringBuilder sb = new StringBuilder();
        dest.writeInt(id);
        dest.writeInt(modifyCount);
        dest.writeInt(cost);
        sb.append(String.valueOf(counts[0]))
                .append("_")
                .append(String.valueOf(counts[1]))
                .append("_")
                .append(String.valueOf(counts[2]))
                .append("_")
                .append(String.valueOf(counts[3]));
        dest.writeString(sb.toString());
    }

    private int id;
    private int modifyCount;
    private int cost;
    private int [] counts;
    public static final Parcelable.Creator<GameRuntimeDataMode> CREATOR =
            new Creator<GameRuntimeDataMode>() {
                @Override
                public GameRuntimeDataMode createFromParcel(Parcel source) {
                    return new GameRuntimeDataMode(source);
                }

                @Override
                public GameRuntimeDataMode[] newArray(int size) {
                    return new GameRuntimeDataMode[size];
                }
            };

    private GameRuntimeDataMode(int id,int [] counts,int cost) {
        this.id = id;
        this.counts = counts;
        this.modifyCount = 0;
        this.cost = cost;
    }
    private GameRuntimeDataMode(Parcel in){
        int [] nCounts = new int [4];
        id = in.readInt();
        modifyCount = in.readInt();
        cost = in.readInt();
        String[] strCounts = in.readString().split("_");
        nCounts[0] = Integer.parseInt(strCounts[0]);
        nCounts[1] = Integer.parseInt(strCounts[1]);
        nCounts[2] = Integer.parseInt(strCounts[2]);
        nCounts[3] = Integer.parseInt(strCounts[3]);
        this.counts = nCounts;
    }
    private GameRuntimeDataMode(int id,int [] counts,int modifyCount,int cost) {
        this.id = id;
        this.counts = counts;
        this.modifyCount = modifyCount;
        this.cost = cost;
    }
    @Override
    public int[] getGameCount() {
        return counts;
    }

    @Override
    public boolean canBeModified() {
        if (modifyCount<MODIFY_MAX)
            return true;
        return false;
    }

    @Override
    public int getCost() {
        return cost;
    }

    @Override
    public boolean doModifying(int[] counts,int cost) {
        synchronized (this){
            for (int i=0;i<4;i++){
                this.counts[i] = counts[i];
            }
            modifyCount++;
            this.cost = cost;
        }
        return true;
    }

    @Override
    public int getModifyCount() {
        return modifyCount;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        GameRuntimeDataMode mode = null;
        try{
            mode = (GameRuntimeDataMode) super.clone();
        }catch (CloneNotSupportedException e){
            e.printStackTrace();
        }
        return mode;
    }

    @Override
    public ContentValues getContentValues() {
        ContentValues contentValues = new ContentValues();
        contentValues.put(String.valueOf(DATA_COLUMN_NAME.COLUMN_COUNT1),counts[0]);
        contentValues.put(String.valueOf(DATA_COLUMN_NAME.COLUMN_COUNT2),counts[1]);
        contentValues.put(String.valueOf(DATA_COLUMN_NAME.COLUMN_COUNT3),counts[2]);
        contentValues.put(String.valueOf(DATA_COLUMN_NAME.COLUMN_COUNT4),counts[3]);
        contentValues.put(String.valueOf(DATA_COLUMN_NAME.COLUMN_COST),cost);
        contentValues.put(String.valueOf(DATA_COLUMN_NAME.COLUMN_MODIFYCOUNT),modifyCount);
        return contentValues;
    }

    @Override
    public int getId() {
        return id;
    }


    public static GameRuntimeDataMode _of(int id,int []counts,int cost){
        return new GameRuntimeDataMode(id,counts,cost);
    }
    public static GameRuntimeDataMode _of(int id,int []counts,int modifyCount,int cost){
        return new GameRuntimeDataMode(id,counts,modifyCount,cost);
    }
    public static GameRuntimeDataMode _of(GameRuntimeDataMode mode){
        int [] res = new int [4];
        int []modeRes = mode.getGameCount();
        for (int i=0;i<res.length;i++){
            res[i] = modeRes[i];
        }
        return new GameRuntimeDataMode(mode.getId(),res,mode.getModifyCount(),mode.getCost());
    }
    public static GameRuntimeDataMode transferCursor(Cursor cursor){
        int [] counts = new int [4];
        int mCount = 0;
        int id;
        int cost;
        id = cursor.getInt(cursor.getColumnIndex(String.valueOf(DATA_COLUMN_NAME.COLUMN_ID)));
        counts[0] = cursor.getInt(cursor.getColumnIndex(String.valueOf(DATA_COLUMN_NAME.COLUMN_COUNT1)));
        counts[1] = cursor.getInt(cursor.getColumnIndex(String.valueOf(DATA_COLUMN_NAME.COLUMN_COUNT2)));
        counts[2] = cursor.getInt(cursor.getColumnIndex(String.valueOf(DATA_COLUMN_NAME.COLUMN_COUNT3)));
        counts[3] = cursor.getInt(cursor.getColumnIndex(String.valueOf(DATA_COLUMN_NAME.COLUMN_COUNT4)));
        mCount = cursor.getInt(cursor.getColumnIndex(String.valueOf(DATA_COLUMN_NAME.COLUMN_MODIFYCOUNT)));
        cost = cursor.getInt(cursor.getColumnIndex(String.valueOf(DATA_COLUMN_NAME.COLUMN_COST)));
        return new GameRuntimeDataMode(id,counts,mCount,cost);

    }

    @Override
    public String toString() {
        return "GameRuntimeDataMode{" +
                "counts=" + Arrays.toString(counts) +
                ", modifyCount=" + modifyCount +
                ", id=" + id +
                ", cost=" + cost +
                '}';
    }
}
